package cn.iocoder.yudao.module.crossborder.platform.service.platformwebhook;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.crossborder.platform.controller.admin.platformwebhook.vo.*;
import cn.iocoder.yudao.module.crossborder.platform.dal.dataobject.platformwebhook.PlatformWebhookDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.crossborder.platform.dal.mysql.platformwebhook.PlatformWebhookMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.crossborder.platform.enums.ErrorCodeConstants.*;

/**
 * 跨境电商平台Webhook配置 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PlatformWebhookServiceImpl implements PlatformWebhookService {

    @Resource
    private PlatformWebhookMapper platformWebhookMapper;

    @Override
    public Long createPlatformWebhook(PlatformWebhookSaveReqVO createReqVO) {
        // 插入
        PlatformWebhookDO platformWebhook = BeanUtils.toBean(createReqVO, PlatformWebhookDO.class);
        platformWebhookMapper.insert(platformWebhook);

        // 返回
        return platformWebhook.getId();
    }

    @Override
    public void updatePlatformWebhook(PlatformWebhookSaveReqVO updateReqVO) {
        // 校验存在
        validatePlatformWebhookExists(updateReqVO.getId());
        // 更新
        PlatformWebhookDO updateObj = BeanUtils.toBean(updateReqVO, PlatformWebhookDO.class);
        platformWebhookMapper.updateById(updateObj);
    }

    @Override
    public void deletePlatformWebhook(Long id) {
        // 校验存在
        validatePlatformWebhookExists(id);
        // 删除
        platformWebhookMapper.deleteById(id);
    }

    @Override
        public void deletePlatformWebhookListByIds(List<Long> ids) {
        // 删除
        platformWebhookMapper.deleteByIds(ids);
        }


    private void validatePlatformWebhookExists(Long id) {
        if (platformWebhookMapper.selectById(id) == null) {
            throw exception(PLATFORM_WEBHOOK_NOT_EXISTS);
        }
    }

    @Override
    public PlatformWebhookDO getPlatformWebhook(Long id) {
        return platformWebhookMapper.selectById(id);
    }

    @Override
    public PageResult<PlatformWebhookDO> getPlatformWebhookPage(PlatformWebhookPageReqVO pageReqVO) {
        return platformWebhookMapper.selectPage(pageReqVO);
    }

}